﻿''' <summary>
''' Configura el area de Trabajo
''' </summary>
''' <remarks>Formulario de Configuración general del sistema</remarks>
''' JS: Juan Antonio Sanjuan juanantonios@gmail.com
Public Class frmConfigurarArea

    ''' <summary>
    ''' Guarda la informacion en un archivo plano
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks> </remarks>
    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        Try
            Dim objConfiguracion As New Configuracion
            objConfiguracion.CadenaConexion = txtCadenaConexion.Text
            objConfiguracion.CapasSQL = txtCapasSQL.Text
            objConfiguracion.Cartografia = txtRuta.Text
            objConfiguracion.IP = txtIP.Text
            objConfiguracion.Puerto = txtPuerto.Text
            objConfiguracion.XInicial = Integer.Parse(txtXInicial.Text)
            objConfiguracion.YInicial = Integer.Parse(txtYInicial.Text)
            objConfiguracion.ZInicial = Integer.Parse(txtZInicial.Text)
            objConfiguracion.ColorCartografia = New Colores(txtColorCartografia.BackColor)
            objConfiguracion.CapaSQLTabla = CType(dgvCapas.DataSource, DataTable)
            guardarConfiguracion(objConfiguracion)
            Me.Hide()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Evento de inicio que Carga los datos del archivo de configuración
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub frmConfigurarArea_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ddlTipoCapa.Items.Add("Linea")
        ddlTipoCapa.Items.Add("Triangulo")
        ddlTipoCapa.Items.Add("Cuadrado")
        ddlTipoCapa.SelectedIndex = 0
        cargarDatos()
    End Sub

    ''' <summary>
    ''' Carga Datos de Configuracion en cada control
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub cargarDatos()
        Dim objConfiguracion As Configuracion = cargarConfiguracion(Nothing)
        objConfiguracion.revisarTabla()
        txtCadenaConexion.Text = objConfiguracion.CadenaConexion
        txtCapasSQL.Text = objConfiguracion.CapasSQL
        txtRuta.Text = objConfiguracion.Cartografia
        txtIP.Text = objConfiguracion.IP
        txtPuerto.Text = objConfiguracion.Puerto
        txtColorCartografia.BackColor = objConfiguracion.ColorCartografia.getWindowsColor()
        txtXInicial.Text = objConfiguracion.XInicial.ToString
        txtYInicial.Text = objConfiguracion.YInicial.ToString
        txtZInicial.Text = objConfiguracion.ZInicial.ToString
        dgvCapas.DataSource = objConfiguracion.CapaSQLTabla
    End Sub

    ''' <summary>
    ''' Evento que despliega la ventana de seleccion de la cartografia
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnCargarCartografia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargarCartografia.Click
        odfDXF.Filter = "Cartografia DXF (*.dxf)|*.dxf|All files (*.*)|*.*"
        If (odfDXF.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            txtRuta.Text = odfDXF.FileName
        End If
    End Sub

    ''' <summary>
    ''' Evento para seleccionar el color de la cartografia
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnColorCartografia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColorCartografia.Click
        If (cldColores.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            txtColorCartografia.BackColor = cldColores.Color
        End If
    End Sub

    ''' <summary>
    ''' Evento para seleccionar el color de la capa
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnColorCapa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColorCapa.Click
        If (cldColores.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            txtColorCapa.BackColor = cldColores.Color
        End If
    End Sub

    ''' <summary>
    ''' Evento para seleccionar una capa nueva
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnNuevaCapa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevaCapa.Click
        Dim objTabla As DataTable = CType(dgvCapas.DataSource, DataTable)
        Dim objRegistro As DataRow = objTabla.NewRow
        objRegistro("Nombre") = txtNombreCapa.Text
        objRegistro("SQL") = txtCapasSQL.Text
        objRegistro("Tipo") = CType(ddlTipoCapa.SelectedItem, String)
        objRegistro("Color") = New Colores(txtColorCapa.BackColor)
        objRegistro("Tamano") = nudTamano.Value
        objTabla.Rows.Add(objRegistro)
        dgvCapas.DataSource = objTabla
        dgvCapas.Refresh()
    End Sub

    ''' <summary>
    ''' Evento para cancelar las operaciones en este formulario
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Hide()
    End Sub

    ''' <summary>
    ''' Evento para eliminar una capa
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnBorrarCapa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBorrarCapa.Click
        Try
            Dim objTabla As DataTable = CType(dgvCapas.DataSource, DataTable)
            Dim strombreCapa As String = dgvCapas.SelectedRows(0).Cells("Nombre").Value.ToString()
            Dim objRegistroTMP As DataRow = Nothing
            For Each objRegistro As DataRow In objTabla.Rows
                If (objRegistro("Nombre").ToString = strombreCapa) Then
                    objRegistroTMP = objRegistro
                    Exit For
                End If
            Next
            objTabla.Rows.Remove(objRegistroTMP)
            dgvCapas.DataSource = objTabla
            dgvCapas.Refresh()
        Catch ex As Exception
        End Try
    End Sub

    ''' <summary>
    ''' Evento para quirat la referencia a la ruta de una cartografia
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnBorrarCartografia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBorrarCartografia.Click
        txtRuta.Text = ""
    End Sub
End Class